<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\ClassBaseNumbering;
use App\Http\Requests\Admin\ClassBaseNumberingController\doRule;

class ClassBaseNumberingController extends Controller
{
    public function index()
    {
        $result = [
            'result'=>ClassBaseNumbering::lists()->paginate(6),
            'sear_value'=>\request()->get('sear_value'),
        ];

        return view('admin.class_base_numbering.index', $result);
    }

    public function add()
    {

        $result = [

        ];

        return view('admin.class_base_numbering.edit',$result);
    }

    public function edit(Request $request, $id)
    {

        $info = ClassBaseNumbering::info($id);

        $result = [
            'id'=>$id,
            'result'=> $info,
        ];

        return view( 'admin.class_base_numbering.edit', $result);

    }

    public function del($id)
    {
        $where = [];

        $where[] = ['class_base_numbering_id', $id];

        if (ClassBaseNumbering::class_bass_lists($where)->get()->isNotEmpty())return self::fail('該課程編號還有課程，請先修改在來刪除');

        ClassBaseNumbering::del($id);

        return self::success([]);

    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            if (empty($input['id'])) {

                unset($input['id']);

                ClassBaseNumbering::add($input);

            } else {

                ClassBaseNumbering::edit($input['id'],$input);

            }

            return self::success();
        }
    }
}
